package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Comments;
import cn.wolfcode.trip.base.domain.Reply;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.CommentQuery;
import cn.wolfcode.trip.base.service.ICommentService;
import cn.wolfcode.trip.base.service.IReplyService;
import cn.wolfcode.trip.base.util.DateUtil;
import cn.wolfcode.trip.base.util.JSONResult;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("comment")
public class CommentController {

    @Autowired
    private ICommentService commentService;
    @Autowired
    private IReplyService replyService;

    //  保存评论
    @PostMapping("/saveOrUpdate")
    public JSONResult saveComment(Comments comment) {
        JSONResult result = new JSONResult();
        try {
            User user = UserContext.getCurrentUser();
            if (user.getHeadImgUrl() != null) {
                comment.setHeadImgUrl(user.getHeadImgUrl());
            }
            comment.setUser_id(user.getId());
            comment.setUsername(user.getNickName());
            commentService.saveOrUpdate(comment);


        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    //  保存回复
    @PostMapping("/{commentId}/saveReply")
    public JSONResult saveReply(@PathVariable Long commentId, Reply reply) {
        JSONResult result = new JSONResult();
        try {
            User user = UserContext.getCurrentUser();
            if (user.getHeadImgUrl() != null) {
                reply.setUser_headImgUrl(user.getHeadImgUrl());
            }
            reply.setUser_id(user.getId());
            reply.setUsername(user.getNickName());
            reply.setId(commentId);
            replyService.saveOrUpdate(reply);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    //当查看完消息后  将所有消息状态置为0 已读
    @PostMapping("/setMessageState")
    public Object setMessageState(Integer state) {
        JSONResult result = new JSONResult();
        try {
            commentService.setMessageState(state);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    //  获取评论
    @GetMapping
    public Object getComment(CommentQuery qo) {
        return commentService.query(qo);
    }

    //  获取一个评论下所有回复者
    @GetMapping("/replyer")
    public Object getUserByComment(Long commentId) {
        return commentService.getUserByComment(commentId);
    }

    // 查询所有评论
    @GetMapping("/getMessages")
    public Object getMessages(Long userId) {
        return commentService.getMessages(userId);
    }

    //  查询一个评论下所有回复
    @GetMapping("/replies")
    public Object getRepliesByCommentId(CommentQuery qo) {
        return replyService.getRepliesByCommentId(qo);
    }

    // 查询所有未读评论
    @GetMapping("/getMessagesByType")
    public Object getMessagesByType(Long userId, HttpServletRequest request, HttpServletResponse response) {
        List<Comments> list = commentService.getMessagesByType(userId);
        // 反正不知道干啥的 返回数据
        JSONArray jsonArray = new JSONArray();
        for (Comments comments : list) {
            JSONObject jsonLog = new JSONObject();
            jsonLog.put("id", comments.getId());
            jsonLog.put("user_id", comments.getUser_id());
            jsonLog.put("target_id", comments.getTarget_id());
            jsonLog.put("targetname", comments.getTargetname());
            jsonLog.put("state", comments.getState());
            jsonLog.put("admire_count", comments.getAdmire_count());
            String create_time = DateUtil.date2string(comments.getCreate_time(), "yyyy-MM-dd HH-mm-ss");
            jsonLog.put("create_time", create_time);

            jsonLog.put("update_time", comments.getUpdate_time());
            jsonLog.put("username", comments.getUsername());
            jsonLog.put("headImgUrl", comments.getHeadImgUrl());
            jsonLog.put("hot_value", comments.getHot_value());
            jsonLog.put("type", comments.getType());
            jsonLog.put("content", comments.getContent());
            jsonArray.add(jsonLog);
        }
        //JSONObject result = new JSONObject();
        //result.put("data",jsonArray);
        /*try {
            response.reset();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json" );

            PrintWriter wr = response.getWriter();
            wr.write(result.toString());
            wr.flush();
            wr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        return jsonArray;
    }

    // 查询自己的所有评论
    @GetMapping("/getMycomments")
    public Object getMycomments(Long userId) {
        return commentService.getMycomments(userId);
    }
}
